#!/bin/sh
#
# Copyright (C) 2000-2022 Kern Sibbald
# License: BSD 2-Clause; see file LICENSE-FOSS
#
TestName="reload-client-address-test"
JobName=backup
. scripts/functions

scripts/cleanup
scripts/copy-confs

#
# Zap out any schedule in default conf file so that
#  it doesn't start during our test
#
outf="$tmp/sed_tmp"
echo "s%  Schedule =%# Schedule =%g" >${outf}
cp $scripts/bacula-dir.conf $tmp/1
sed -f ${outf} $tmp/1 >$scripts/bacula-dir.conf

change_jobname BackupClient1 $JobName
start_test

$bperl -e 'add_attribute("$conf/bacula-dir.conf", "Address", "127.0.0.1", "Client")'
$bperl -e 'add_attribute("$conf/bacula-dir.conf", "Enabled", "yes", "Client")'

cat <<END_OF_DATA >$tmp/bconcmds
@$out /dev/null
messages
@$out $tmp/log0.out
show client
@$out $tmp/log1.out
run job=$JobName yes
wait
messages
quit
END_OF_DATA

run_bacula

print_debug "Changing 127.0.0.1 to 127.0.0.2 for the Client"
$bperl -e 'add_attribute("$conf/bacula-dir.conf", "Address", "127.0.0.2", "Client")'

cat <<END_OF_DATA >$tmp/bconcmds
@$out $tmp/log2.out
reload
messages
show client
quit
END_OF_DATA

run_bconsole

print_debug "Disable client"

client=`awk '/Name=/ { gsub(/Name=/, "", $2); print $2; exit }' $tmp/log0.out`

cat <<END_OF_DATA >$tmp/bconcmds
@$out $tmp/log3.out
disable client=$client
show client
@$out $tmp/log4.out
reload
show client
@$out $tmp/log5.out
enable client=$client
reload
show client
quit
END_OF_DATA

run_bconsole

$bperl -e 'add_attribute("$conf/bacula-dir.conf", "Enabled", "no", "Client")'

cat <<END_OF_DATA >$tmp/bconcmds
@$out $tmp/log6.out
reload
show client
@$out $tmp/log7.out
enabled client=$client
reload
show client
quit
END_OF_DATA

grep "127.0.0.1" $tmp/log0.out > /dev/null
if [ $? -ne 0 ]; then
    print_debug "ERROR: Should find 127.0.0.1 in $tmp/log0.out"
    estat=1
fi

grep "127.0.0.2" $tmp/log2.out > /dev/null
if [ $? -ne 0 ]; then
    print_debug "ERROR: Should find 127.0.0.2 in $tmp/log2.out"
    estat=1
fi

grep "127.0.0.2" $tmp/log3.out > /dev/null
if [ $? -ne 0 ]; then
    print_debug "ERROR: Should find 127.0.0.2 in $tmp/log3.out"
    estat=1
fi

grep "Enabled=0" $tmp/log3.out > /dev/null
if [ $? -ne 0 ]; then
    print_debug "ERROR: Should find Enabled=0 in $tmp/log3.out"
    estat=1
fi

grep "Enabled=0" $tmp/log4.out > /dev/null
if [ $? -ne 0 ]; then
    print_debug "ERROR: Should find Enabled=0 in $tmp/log4.out"
    estat=1
fi

grep "Enabled=1" $tmp/log5.out > /dev/null
if [ $? -ne 0 ]; then
    print_debug "ERROR: Should find Enabled=1 in $tmp/log5.out"
    estat=1
fi

stop_bacula

end_test
